package b3.widget.tab
{
	import b3.net.*;
	import b3.scene.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.ui.Mouse;

	public class TabPageSelector extends Sprite
	{
		public var firstPage:Sprite;
		public var lastPage:Sprite;
		public var pageUp:Sprite;
		public var pageDown:Sprite;
		public var pageNumArea:TabPageNumArea;
		private var container:Sprite;
		private const gap:int = 5;
		private const tabType:int = 2;
		private var currentPageNumColor:uint;
		private var pageNumColor:uint;
		
		private var tab:Tab;
		
		public function TabPageSelector(tab:Tab, TabPageFirstPage:Class = null, TabPagePageUp:Class = null, TabPagePageDown:Class = null, TabPageLastPage:Class = null, 
			pageNumColor:uint = 0xFFFF00, currentPageNumColor:uint = 0xFFFF00)
		{
			super();
			this.tab = tab;
			this.pageNumColor = pageNumColor;
			this.currentPageNumColor = currentPageNumColor;
			if (TabPageFirstPage == null)
				TabPageFirstPage = ResourceHelper.getInstance().getClassByName("TabPageFirstPage");
			if (TabPagePageUp == null)
				TabPagePageUp = ResourceHelper.getInstance().getClassByName("TabPagePageUp");
			if (TabPagePageDown == null)
				TabPagePageDown = ResourceHelper.getInstance().getClassByName("TabPagePageDown");
			if (TabPageLastPage == null)
				TabPageLastPage = ResourceHelper.getInstance().getClassByName("TabPageLastPage");
			container = new Sprite();
			addChild(container);
			//首页
			firstPage = new TabPageFirstPage();
			firstPage.mouseChildren = false;
			container.addChild(firstPage);
			firstPage.addEventListener(MouseEvent.CLICK, onFirstPageClick);
			firstPage.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			firstPage.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			var bottomNavigatorPosYGap:int = 20;
			//上一页
			pageUp = new TabPagePageUp();
			pageUp.mouseChildren = false;
			pageUp.x = firstPage.width + gap;
			container.addChild(pageUp);
			pageUp.addEventListener(MouseEvent.CLICK, onPageUpClick);
			pageUp.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			pageUp.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			pageNumArea = new TabPageNumArea(tab, pageNumColor, currentPageNumColor);
			pageNumArea.x = pageUp.x + pageUp.width + gap;
			container.addChild(pageNumArea);
			//下一页
			pageDown = new TabPagePageDown();
			pageDown.mouseChildren = false;
			container.addChild(pageDown);
			pageDown.addEventListener(MouseEvent.CLICK, onPageDownClick);
			pageDown.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			pageDown.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			//末页
			lastPage = new TabPageLastPage();
			lastPage.mouseChildren = false;
			container.addChild(lastPage);
			lastPage.addEventListener(MouseEvent.CLICK, onLastPageClick);
			lastPage.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			lastPage.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		public function hideNumArea():void
		{
			pageNumArea.visible = false;
		}
		
		public function refreshTotalPageNum():void
		{
			if (pageNumArea.visible)
			{
				if (pageNumArea != null && pageNumArea.parent != null)
					pageNumArea.parent.removeChild(pageNumArea);
				pageNumArea = new TabPageNumArea(tab, pageNumColor, currentPageNumColor);
				pageNumArea.x = pageUp.x + pageUp.width + gap;
				pageDown.x = pageNumArea.x + pageNumArea.width + gap;
				lastPage.x = pageDown.x + pageDown.width + gap;
				container.addChild(pageNumArea);
				container.x = -container.width / 2;
			}
		}
		
		private function onFirstPageClick(event:MouseEvent):void
		{
			if (tab.currentPageIndex > 0)
			{
				if (tabType == 0 || tabType == 3)
					pageNumArea.refreshCurrentPageIndex(0);
				tab.currentPageIndex = 0;
			}
		}
		
		private function onPageUpClick(event:MouseEvent):void
		{
			if (tab.currentPageIndex > 0)
			{
				if (tabType == 0 || tabType == 3)
					pageNumArea.refreshCurrentPageIndex(tab.currentPageIndex - 1);
				tab.currentPageIndex--;
			}
		}
		
		private function onPageDownClick(event:MouseEvent):void
		{
			if (tab.currentPageIndex < tab.totalPageNum - 1)
			{
				if (tabType == 0 || tabType == 3)
					pageNumArea.refreshCurrentPageIndex(tab.currentPageIndex + 1);
				tab.currentPageIndex++;
			}
		}
		
		private function onLastPageClick(event:MouseEvent):void
		{
			if (tab.currentPageIndex < tab.totalPageNum - 1)
			{
				if (tabType == 0 || tabType == 3)
					pageNumArea.refreshCurrentPageIndex(tab.totalPageNum - 1);
				tab.currentPageIndex = tab.totalPageNum - 1;
			}
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			event.target.filters = [new ColorMatrixFilter(
				[1,0,0,0,-15,
			     0,1,0,0,-15,
			     0,0,1,0,-15,
			     0,0,0,1,0 ])];
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			event.target.filters = null;
		}
	}
}